{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">操作提示</h2>
        <ul class="layui-colla-content layui-show">
            <li>您可以在此添加酒店的房间类型</li>
        </ul>
    </div>
</div>

<div class="ns-single-filter-box">
    <button class="layui-btn ns-bg-color" onclick="add()">添加房间类型</button>

    <div class="layui-form">
        <div class="layui-input-inline ns-len-mid">
            <input type="text" name="search_text" placeholder="请输入房型名称" class="layui-input">
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
                <i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>

</div>

<table id="adv_list" lay-filter="adv_list"></table>


<!-- 操作 -->
<script type="text/html" id="action">
    <div class="ns-table-btn">
        <a class="layui-btn" lay-event="edit">编辑</a>
        <a class="layui-btn" lay-event="delete">删除</a>

    </div>
</script>


{/block}
{block name="script"}
<script>
    layui.use('form', function () {
        var table,
            form = layui.form,
            repeat_flag = false; //防重复标识
        form.render();

        table = new Table({
            elem: '#adv_list',
            url: ns.url("hotel://hotel/roomtype/lists"),
            cols: [
                [  {
                    field: 'type_name',
                    title: '房型名称',
                    unresize: 'false',
                    width: '15%'
                },{
                    title: '创建时间',
                    unresize: 'false',
                    width: '12%',
                    templet: function (res) {
                        if (res.create_time == 0) {
                            return '--';
                        } else {
                            return ns.time_to_date(res.create_time)
                        }
                    }
                },  {
                    title: '操作',
                    toolbar: '#action',
                    unresize: 'false',
                    width: '12%'
                }]
            ],

        });

        /**
         * 监听工具栏操作
         */
        table.tool(function(obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit': //编辑
                    location.href = ns.url("hotel://hotel/roomtype/edit",{"type_id":data.type_id});
                    break;
                case 'delete': //删除
                    deleteRoom(data.type_id);
                    break;
            }
        });



        /**
         * 删除
         */
        function deleteRoom(type_ids) {
            if (repeat_flag) return false;
            repeat_flag = true;

            layer.confirm('确定要删除该房间类型吗?', function() {
                $.ajax({
                    url: ns.url("hotel://hotel/roomtype/delete"),
                    data: {'type_ids': type_ids},
                    dataType: 'JSON',
                    type: 'POST',
                    success: function(res) {
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

        /**
         * 监听搜索
         */
        form.on('submit(search)', function(data){
            table.reload({
                page: {
                    curr: 1
                },
                where: data.field
            });
        });
    });



    function add() {
        location.href = ns.url("hotel://hotel/roomtype/add");
    }
</script>
{/block}